<?php
class DishModel extends CI_Model{
	public function __construct(){
		parent::__construct();
	}
	
	/*
	 * get number of dishes:
	 */
	function dish_count(){
		return $this->db->count_all("dishes");
	}
	
	/*
	 * get list all dishes from db:
	 */
	function fetch_dishes($limit, $start){
		$this->db->limit($limit, $start);
		$this->db->select('d._id as _id, d.name as name, dt.name as type, material, cooking, speciality, unit, cost, image');
		$this->db->from('dishes as d');
		$this->db->join('dish_types as dt', 'dt._id = d.type_id');
        
		//run select
		$query = $this -> db -> get();
 
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
	}
	
	/*
	 * get list all dishes from db:
	 */
	function type_dish(){
        $query = $this->db->get("dish_types");
 
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
	}
	
	public function get_type_id($type){
		$this->db->select('_id');
		$this->db->from('dish_types');
		$this->db->where('name', $type);
		
		$query = $this->db->get();
		
		if ($query->num_rows() > 0) {
			$result = $query->result();
			return $result[0]->_id;
		}
		
		return false;
	}
	
	/*
	 * create new dish:
	 */
	function create_new_dish($dishData){	
		return $this->db->insert('dishes', $dishData);
	}
	
	/*
	 * create new dish
	 */
	public function new_dish_type($typeData){
		return $this->db->insert('dish_types', $typeData);
	}
	
	/*
	 * dish detail
	 */
	public function dish_detail($dish_id){
		$this->db->select('d._id as _id, d.name as name, d.type_id as dype_id, dt.name as type, material, cooking, speciality, unit, cost, image');
		$this->db->from('dishes as d');
		$this->db->join('dish_types as dt', 'dt._id = d.type_id');
		$this->db->where('d._id', $dish_id);
		
		//run select
		$query = $this -> db -> get();
 
        if ($query->num_rows() > 0) {
            $result = $query->result();
            return $result[0];
        }
        return false;
	}
	
	/*
	 * update new dish:
	 */
	public function update_dish($dish_id, $info){
		$this->db->where('_id', $dish_id);
		$this->db->update('dishes', $info);
		$affect = $this->db->affected_rows();

		if ($affect > 0){
			return true;
		}else{
			return false;
		}
	}
	
	/*
	 * delete dish
	 */
	public function delete_dish($dish_id){
		$this->db->where('_id', $dish_id);
		$this->db->delete('dishes');
	}
	
	public function all_dish(){
		$this->db->select('d._id as _id, d.name as name, d.key_search as key_search, d.type_id as type_id, dt.name as type, material, cooking, speciality, unit, cost, image');
		$this->db->from('dishes as d');
		$this->db->join('dish_types as dt', 'dt._id = d.type_id');
        
		//run select
		$query = $this -> db -> get();
 
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
	}
	
	public function all_dish_types(){
		$this->db->select('_id, name');
		$this->db->from('dish_types');
		
		//run select
		$query = $this -> db -> get();
 
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
	}
}